package com.pzhu.service.impl;

import com.pzhu.mapper.BrandMapper;
import com.pzhu.pojo.Brand;
import com.pzhu.service.BrandService;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import util.SqlSessionFactoryUtil;

import java.util.List;

/**
 * @author Yancy xiao
 * @create 2023-04-03 8:40
 */
public class BrandServiceImpl implements BrandService {
    public List<Brand> selectAll() {
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();

        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        List<Brand> brands = brandMapper.selectAll();
        sqlSession.close();
        return brands;
    }

    @Override
    public void add(Brand brand) {
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();

        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        brandMapper.add(brand);
        sqlSession.commit();
        sqlSession.close();
    }

    public void deletByIds(int[] ids) {
        //自定义一个SqlSessionFactoryUtil类来通过静态方法获取sqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactory();
        //2.获取sqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3.获取代理对象
        BrandMapper brandeMapper = sqlSession.getMapper(BrandMapper.class);
        //4.调用批量删除方法做删除
        brandeMapper.deleteByIds(ids);
//        5提交事物
        sqlSession.commit();
        //释放资源
        sqlSession.close();
    }
}
